If you look in Resources, you’ll see links to the complete books: R for Data Science (R4DS) and Data Visualization: A Practical Introduction (DV)
Download the RMarkdown template from the class website. The file follows this homework, and contains some exemplar code, some fill-in-the-blanks, and some space for your R code contributions. Open it in RStudio (using File > Open). Only enter your R code in R chunks, that is,
Once you’re done, you will upload the RMarkdown file on the class website. I will run your RMarkdown to see how you did on the different questions. Look for a screencast that describes the process of downloading the template, using it, and then uploading it.
Since I was late in posting, you will have till Thursday, Feb 20 to do this, and so you can ask questions next week in class.
If you look at lecture 2, we imported several datasets into R. For this homework, you need to import the gapminder data into R. It is available on the class website as gapminder.csv. Save the file in the data folder of the class RStudio Project you created. You will use the import function from the rio package.
## Fill in the blanks
library(_______)
gapm <- import("________________")
We need to do the library() call to activate the package, so that R knows where to look for the function.
What happens if you just use import and not assign it to a name?
Let’s look at population growth over time.
ggplot(data = gapm,
mapping = aes(x = year, y = pop))+
geom_line(color = country, show.legend = F)+
scale_y_log10()
## Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomLine, : object 'country' not found
Oops!! This doesn’t quite work.
a. Update the code above so that the following plot is rendered.
b. This has too many colors, in my opinion. Lets just color the lines by continent rather than country, and allow the legend to also be drawn. The desired plot is as follows:
Hint: you need to use both group and color, possibly with different variables in the data set
c. This is still too messy. Let’s just separate out the continents and see the population growth by country.
d. Okay. Let’s go with this as the final graph. Let’s clean it up with proper labels on the x- and y-axes, changing the numbers on the y-axis from scientific notation to usual numbers, and adding a title. The layers you’ll need to add involve
labs(x = ___, y = ___) for the x and y labelsscale_y_log10 function (see the lecture)ggtitle to add a titleThe final picture should look something like this (you can decide on your own labels and title). Look at the documnetation for the layers and functions you use to see what options are available to make the plot look like mine.
Now, let’s look at the relationship between per capita GDP of a country and the life expectancy in the country in the gapminder dataset. The final visualization will mimic (with more limited data) a famous visualization that was shown in the Hans Rosling video The Joy of Stats
Let’s start out by plotting the life expectancy against per capita GDP in a scatter plot
ggplot(gapm, aes(x = gdpPercap, y = lifeExp)) +
geom_point()
We’re going to successively build up this visualization by adding (+) layers to the plot. Don’t create a legend for this plot
a. Change the x-axis to display on the log scale
b. Change the size of the points to reflect the population
c. Color the inside of each point by continent
d. Separate the graphs out by year (use facet_wrap)
The result should look something like this:
I will be impressed if you can get the annotation on the axis like mine. A good resource for this is this page, scrolling down to the section titled “Graphical parameters”. You should also see the documentation of the functions in the scales package (In the lower right pane, there is a pane marked “Packages”. There you can search the name of a package and see all the available functions and then click to get the individual function documentations)
Make the following visualization using gganimate based on the plot you developed in Question 3. Note that it might take a minute, literally, for the animation to render. You can see a very close example here